package com.coc.service;

import com.coc.DTO.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * String Data Redis 为我们提供了 RedisTemplate
 * 和 StringRedisTemplate 两个模版来进行数据操作，
 * 其中：StringRedisTemplate 只针对键值都是字符串的数据进行操作。
 */
@Service
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void setValue () {

        redisTemplate.opsForValue().set("key1","value1");
        stringRedisTemplate.opsForValue().set("key2","value2");
        System.out.println("设置 key1 : value1 ");

        redisTemplate.opsForValue().set("u1",getUser());
        

    }

    public User getUserFromRedis () {
        return (User) redisTemplate.opsForValue().get("u1");
    }

    public String getValue () {
        return (String)redisTemplate.opsForValue().get("key1");
    }

    public String deleteValue(){
        redisTemplate.delete("key1");
        return "success";

    }

    private User getUser () {
        User u = new User();
        u.setName("独孤求败");
        u.setPass("qwer123");
        u.setAge(18);
        u.setAddress("火云洞");
        return u;
    }

}
